package com.turnengine.client.common.command.executor.xstream;

import java.io.InputStream;
import java.io.OutputStream;

import com.javabi.command.ICommandRequestList;
import com.javabi.command.ICommandResponseList;
import com.javabi.command.errorcode.ErrorCodeAttributes;
import com.javabi.command.errorcode.ErrorCodeSet;
import com.javabi.command.response.CommandRequestList;
import com.javabi.command.response.CommandResponseList;
import com.javabi.common.xml.ConcurrentXStream;
import com.turnengine.client.common.command.client.ICommandStream;

public class CommandXStream implements ICommandStream {

	private final ConcurrentXStream xstream = new ConcurrentXStream();

	public CommandXStream() {
		synchronized (xstream) {
			xstream.alias(CommandResponseList.class);
			xstream.alias(CommandRequestList.class);
			xstream.alias(ErrorCodeSet.class);
			xstream.alias(ErrorCodeAttributes.class);
		}
	}

	@Override
	public void writeRequestList(ICommandRequestList requestList, OutputStream output) {
		xstream.toXML(requestList, output);
	}

	@Override
	public ICommandResponseList readResponseList(InputStream input) {
		return (ICommandResponseList) xstream.fromXML(input);
	}

	@Override
	public void writeResponseList(ICommandResponseList responseList, OutputStream output) {
		xstream.toXML(responseList, output);
	}

	@Override
	public ICommandRequestList readRequestList(InputStream input) {
		return (ICommandRequestList) xstream.fromXML(input);
	}

	@Override
	public String toString(ICommandRequestList requestList) {
		return xstream.toXML(requestList);
	}

	@Override
	public String toString(ICommandResponseList responseList) {
		return xstream.toXML(responseList);
	}

}
